/*
 * AbstractFactoryImpl.java
 * 
 * Created on Jul 3, 2007, 1:39:48 PM
 * 
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.abstraction;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @param Content 
 * @author DGronau
 */
public class AbstractFactoryImpl<Content> implements AbstractFactory<Content> {

    private Map<String, Factory<Content>> map = new HashMap<String, Factory<Content>>();
    
    /**
     * Registers a MapLoader to be called with the given extension.
     * @param ext The File extension
     * @param loader The MapLoader
     */
    public void register(String ext, Factory<Content> loader) {
        map.put(ext.toLowerCase(), loader);
    }
    
    /**
     * Returns the "known" extensions.
     * @return A String list of all registered extensions (in lowercase)
     */
    public String[] getKeys() {
        String[] extensions = new String[map.size()];
        int i = 0;
        for (String s: map.keySet()) {
            extensions[i++] = s;
        }
        return extensions;
    }

    public void unregister(String key) {
        map.remove(key);
    }

    public void unregisterAll() {
        map.clear();
    }

    public Factory<Content> getFactory(String key) {
        return map.get(key);
    }
}
